﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using ZK.Common;
using ZK.HardWareHelper;

namespace WindowsService_AssistRoad
{
    public partial class Service1 : ServiceBase
    {
        #region 属性
        CameraHelper cameraHelperQ = new CameraHelper(1);//初始化前摄像头
        CameraHelper cameraHelperH = new CameraHelper(2);//初始化后摄像头
        LEDHelper lEDHelperQ = new LEDHelper();//初始化前摄像头
        /// <summary>
        /// 小车道led显示内容初始化timer
        /// </summary>
        System.Timers.Timer t = new System.Timers.Timer(Convert.ToInt32(AppConfigHelper.GetAppSettings("FDLedTimer")));
        /// <summary>
        /// led font size set
        /// </summary>
        int ledFontSize = 10;
        #endregion
        public Service1()
        {
            InitializeComponent();
            DelegateHelper.EventGetCarNum += GetCarNum;
            DoSomething();
            t.Elapsed += new System.Timers.ElapsedEventHandler(ShowRecv);
            t.AutoReset = false;//设置是执行一次（false）还是一直执行(true)；
            t.Enabled = false;//是否执行System.Timers.Timer.Elapsed事件；
        }

        private void ShowRecv(object sender, ElapsedEventArgs e)
        {
            LedShow("欢迎光临");
        }

        /// <summary>
        /// 获取车牌号-委托-事件
        /// </summary>
        /// <param name="carNum"></param>
        /// <param name="order">摄像头顺序（1对外，2对地磅。）</param>
        private void GetCarNum(string carNum, int order)
        {
            string str = order == 1 ? "入口" : "出口";
            WriteLog(str + "-扫到车牌->" + carNum);
            var carDto = WebApiService.CsSmallCar(carNum);
            WriteLog("小车信息->" + JsonOrObjectHelper.ObjectToJson(carDto));
            if (!carDto.success)
            {
                LedShow(carNum + ":无权限");
                t.Start();
                return;
            }
            else
            {
                if (carDto.data.carState == 1)
                {
                    LedShow(carNum + ":黑名单");
                    t.Start();
                    return;
                }
                LedShow("欢迎光临");
                cameraHelperQ.BarrierGateControl(1);
            }
        }
        /// <summary>
        /// led show information
        /// </summary>
        /// <param name="str"></param>
        private void LedShow(string str) {
           bool tempLedPlay =  lEDHelperQ.LedPaly(str, ledFontSize);
            WriteLog(" lEDHelperQ.LedPaly("+str+")->"+ tempLedPlay);
        }
        /// <summary>
        /// 服务所要做的事情。
        /// </summary>
        private void DoSomething()
        {

            var resQ = cameraHelperQ.ManualOne(null);
            if (!resQ.res)
                WriteLog(resQ.Msg + "->打开视频");
            else
            {
                WriteLog("视频打开成功->" + EnumHelper.LCCWLX.information);
            }
            var resH = cameraHelperH.ManualOne(null);
            if (!resH.res)
                WriteLog(resH.Msg + "->打开视频");
            else
            {
                WriteLog("视频打开成功->" + EnumHelper.LCCWLX.information);
            }
            bool ledInstall = lEDHelperQ.LedSmallInitialize(1);
            WriteLog(" lEDHelperQ.LedSmallInitialize(1)->" + ledInstall);
            LedShow("欢迎光临");
        }
        protected override void OnStart(string[] args)
        {
            this.WriteLog("辅道程序OnStart");
        }

        protected override void OnStop()
        {
            this.WriteLog("辅道程序OnStop");
        }
        protected override void OnShutdown()
        {
            this.WriteLog("辅道程序OnShutdown");
        }
      
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="msg"></param>
        private void WriteLog(string msg)
        {
            //string path = @"C:\log.txt";
            //该日志文件会存在windows服务程序目录下
            string pathOfTime = DateTime.Now.ToString("yyyy-MM-dd");
            string path = AppDomain.CurrentDomain.BaseDirectory + "\\assistRoadLog\\" + pathOfTime + ".txt";
            FileInfo file = new FileInfo(path);
            if (!file.Exists)
            {
                FileStream fs;
                fs = File.Create(path);
                fs.Close();
            }

            using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(DateTime.Now.ToString() + "   " + msg);
                }
            }
        }

    }
}
